pacman::p_load("tidyverse","data.table","margins","estimatr","ggpubr","xtable","ggcorrplot","gridExtra","TOSTER","stargazer")
load("Data/Experiment_merged.Rdata")

#Figure 5 using preregistered modelling strategy-----------------------
#select variables
merged_complete <- (dplyr::select(merged,race,gender,age,education,passed,id,general,econ,cult,libcon01, pid01, approval01, politics2nd,symbolic,opennessr,conscientiousness,extraversion,agreeableness,neuroticism))
#create long dataset
merged_l <- as.data.frame(data.table::melt(data.table(merged_complete),measure.vars=list(x=c("opennessr","conscientiousness")),id.vars=c("id","politics2nd","passed","gender","age","race","education","symbolic","general","cult","econ", "libcon01", "pid01", "approval01")))

#general ideology
overal <- lm_robust(value~general*politics2nd+variable+gender+age+race+education,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")

out1 <- margins(overal,variables = "general",at=list(politics2nd=c(0,1)))
general <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='General'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='General'))

#Symbolic
overal_symbolic <- (lm_robust(value~symbolic*politics2nd+variable+gender+age+race+education,subset(merged_l,passed==1),clusters = id,se_type = "stata"))
out1 <- margins(overal_symbolic,variables = "symbolic",at=list(politics2nd=c(0,1)))
symbolic <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Symbolic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Symbolic'))

#pid
overal_pid <- (lm_robust(value~pid01*politics2nd+variable+gender+age+race+education,subset(merged_l,passed==1),clusters = id,se_type = "stata"))
out1 <- margins(overal_pid,variables = "pid01",at=list(politics2nd=c(0,1)))
pid <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Party Identity'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Party Identity'))

#ideology
overal_ideology <- (lm_robust(value~libcon01*politics2nd+variable+gender+age+race+education,subset(merged_l,passed==1),clusters = id,se_type = "stata"))
out1 <- margins(overal_ideology,variables = "libcon01",at=list(politics2nd=c(0,1)))
ideology <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Ideology'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Ideology'))

#approval
overal_approval <- (lm_robust(value~approval01*politics2nd+variable+gender+age+race+education,subset(merged_l,passed==1),clusters = id,se_type = "stata"))
out1 <- margins(overal_approval,variables = "approval01",at=list(politics2nd=c(0,1)))
approval <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Trump approval'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Trump approval'))

#cultural conservatsimn
overal_cult <- (lm_robust(value~cult*politics2nd+variable+gender+age+race+education,subset(merged_l,passed==1),clusters = id,se_type = "stata"))
out1 <- margins(overal_cult,variables = "cult",at=list(politics2nd=c(0,1)))
culture <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Culture'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Culture'))

#economic conservatism
overal_econ <- (lm_robust(value~econ*politics2nd+variable+gender+age+race+education,subset(merged_l,passed==1),clusters = id,se_type = "stata"))
out1 <- margins(overal_econ,variables = "econ",at=list(politics2nd=c(0,1)))
econ <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Economic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Economic'))

#build plot
forplot <- rbind(general, symbolic, pid, ideology, approval, culture, econ)
#extract p-values and calculate one-sided p-values
pvalues<-c(summary(overal)$coefficients[13,4]/2, summary(overal_symbolic)$coefficients[13,4]/2, summary(overal_pid)$coefficients[13,4]/2, summary(overal_ideology)$coefficients[13,4]/2,summary(overal_approval)$coefficients[13,4]/2,  summary(overal_cult)$coefficients[13,4]/2,  summary(overal_econ)$coefficients[13,4]/2)
annotation_df <- data.frame(predictor=c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"),
                            start=c("Internet"),
                            end=c("Politics"),
                            y=c(.22),
                            label=round(pvalues,2)) #if p-values with stars just p-values
annotation_df$predictor <- factor(annotation_df$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))
forplot$predictor <- factor(forplot$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))

main<-ggplot(forplot,aes(y=ame,x=condition))+facet_wrap(~predictor,scales = "fixed", ncol=2) + xlab("Condition") + ylab("Average Marginal Effect") +geom_pointrange(aes(ymin=lower,ymax=upper))+theme_bw(base_size = 25) + geom_signif(data=annotation_df, aes(xmin=start, xmax=end, annotations=label, y_position=y), textsize = 3, vjust = -0.2, manual=TRUE)+ylim(0.00,.25)
ggsave(main, file="Results/main_results_prereg.pdf",width=8,height=10)

#SI 5.3 - Descriptives-------------
select<-c("opennessr","conscientiousness","extraversion","agreeableness","neuroticism", "general", "symbolic",  "libcon01", "pid01", "approval01","cult", "econ", "passed")
descr<-merged[select]
descr<-subset(descr, passed==1)
descr<-descr[-c(13)]

descriptives<-as.data.frame( t(sapply(descr, function(cl) list(Mean=mean(cl,na.rm=TRUE), sd=sd(cl,na.rm=TRUE), N=sum(is.na(cl)==FALSE))) ))
colnames(descriptives)<-c("Mean", "Standard Deviation", "N")
rownames(descriptives)<-c("Openness", "Conscientiousness", "Extraversion", "Agreeableness", "Neuroticism", "Global conservatism", "Symbolic", "Ideology", "Party Identity", "Trump approval", "Cultural", "Economic")
descrip_table <-xtable(descriptives, label = "tab:exp_descriptives",  include.rownames=F,  digits=c(0,2,2,0), caption="Descriptive Statistics of Measures in the Experiment")
print(descrip_table, file="Results/appendix_exp_descri.tex",align=c("lccc"), caption.placement="top")

#covariates
select<-c("age","gender","education","race", "passed")
descr<-merged[select]
descr<-subset(descr, passed==1)

summary(descr$age)
sd(descr$age, na.rm=T)
table(descr$gender)
(752*100)/(781+752+9)
(781*100)/(781+752+9)
(9*100)/(781+752+9)

table(descr$education)
(918*100)/(918+382+242)
table(descr$race)
(1042*100)/(1042+136+149+216)

#SI 5.4 - Psychometric properties of the Big Five traits and ideology  measures-------------
psyc_personality <- rbind(dplyr::select(merged, bfioriginal, bfiartistic, bfiimagination, bfic1, bfic2, bfic3, bfie1, bfie2, bfie3, bfia1, bfia2, bfia3, bfin1, bfin2, bfin3))

alphas <-
  list(
    psych::alpha(data.frame(psyc_personality$bfioriginal, psyc_personality$bfiartistic, psyc_personality$bfiimagination))[[1]],
    psych::alpha(data.frame(psyc_personality$bfic1, psyc_personality$bfic2, psyc_personality$bfic3))[[1]],
    psych::alpha(data.frame(psyc_personality$bfie1, psyc_personality$bfie2, psyc_personality$bfie3))[[1]],
    psych::alpha(data.frame(psyc_personality$bfia1, psyc_personality$bfia2, psyc_personality$bfia3))[[1]],
    psych::alpha(data.frame(psyc_personality$bfin1, psyc_personality$bfin2, psyc_personality$bfin3))[[1]]
  )
alphas <- do.call("rbind",alphas)[,c(2,4)]

ideology_alpha<- dplyr::select(merged, gaymarriage, abortion, immigrants, college, resp1, resp2, approval, libcon, pid, passed)
ideology_alpha <- subset(ideology_alpha, passed==1)

alphas_ideology <-
  list(
    psych::alpha(data.frame(ideology_alpha$gaymarriage, ideology_alpha$abortion, ideology_alpha$immigrants, ideology_alpha$college, ideology_alpha$resp1,ideology_alpha$resp2, 6-ideology_alpha$approval,ideology_alpha$libcon,ideology_alpha$pid))[[1]],
    psych::alpha(data.frame(6-ideology_alpha$approval,ideology_alpha$libcon,ideology_alpha$pid))[[1]],
    psych::alpha(data.frame(ideology_alpha$gaymarriage, ideology_alpha$abortion, ideology_alpha$immigrants, ideology_alpha$college, 6-ideology_alpha$approval,ideology_alpha$libcon,ideology_alpha$pid))[[1]],
    psych::alpha(data.frame(ideology_alpha$resp1,ideology_alpha$resp2))[[1]]
  )

alphas_ideo <- do.call("rbind",alphas_ideology)[,c(2,4)]
alphas_measures <- rbind(alphas, alphas_ideo)

rownames(alphas_measures)<-c("Openness","Conscientiousness","Extraversion","Agreeableness","Neuroticism", "General conservatism", "Symbolic", "Cultural", "Economic")
colnames(alphas_measures) <- c("Alpha","Average r")
alpha_table <-xtable(alphas_measures,  include.rownames=F, label = "tab:experiment_alphas", digits=c(0,2,2), caption="Reliability of Personality and Ideology Measures")
print(alpha_table, file="Results/appendix_experiment_alphas.tex",align=c("lccc"), caption.placement="top")

#SI 5.4 - Correlation between variables----------------
pers_cor <- (dplyr::select(merged,opennessr, conscientiousness, extraversion, agreeableness, neuroticism, passed))
pers_cor <- subset(pers_cor, passed==1)
pers_cor <- pers_cor[-c(6)]
pers_cor <- cor(pers_cor,use="pairwise.complete.obs")
rownames(pers_cor) <- c("Openness","Conscientiousness","Extraversion","Agreeableness","Neuroticism")
colnames(pers_cor) <- c("Openness","Conscientiousness","Extraversion","Agreeableness","Neuroticism")
a<-ggcorrplot(pers_cor, type="lower", show.diag = TRUE, lab=TRUE, lab_size = 4 , title="A. Correlation between personality traits")

ideology_cor <- (dplyr::select(merged,general, symbolic, libcon01, pid01, approval01, cult, econ, passed))
ideology_cor <- subset(ideology_cor, passed==1)
ideology_cor <- ideology_cor[-c(8)]
ideology_cor <- cor(ideology_cor,use="pairwise.complete.obs")
rownames(ideology_cor) <- c("General", "Symbolic", "Ideology", "Party Identity", "Trump approval", "Cultural", "Economic")
colnames(ideology_cor) <- c("General", "Symbolic",  "Ideology", "Party Identity", "Trump approval",  "Cultural", "Economic")
b<-ggcorrplot(ideology_cor, type="lower", show.diag = TRUE, lab=TRUE, lab_size = 4 , title="B. Correlation between ideology variables")

semplot <- grid.arrange(a,b)
ggsave(semplot,file="Results/experiment_cors.png",width=8,height=8)

#SI 5.5 - Randomization checks ------------

#general conservatism
TOSTtwo(m1=mean(merged$general[merged$passed==1 & merged$politics2nd==0], na.rm=T), m2=mean(merged$general[merged$passed==1 & merged$politics2nd==1], na.rm=T), sd1=sd(merged$general[merged$passed==1 & merged$politics2nd==0], na.rm=T), sd2=sd(merged$general[merged$passed==1 & merged$politics2nd==1], na.rm=T), n1=767, n2=776, low_eqbound_d=-0.2, high_eqbound_d=0.2, alpha = 0.05)
#symbolic ideology
TOSTtwo(m1=mean(merged$symbolic[merged$passed==1 & merged$politics2nd==0], na.rm=T), m2=mean(merged$symbolic[merged$passed==1 & merged$politics2nd==1], na.rm=T), sd1=sd(merged$symbolic[merged$passed==1 & merged$politics2nd==0], na.rm=T), sd2=sd(merged$symbolic[merged$passed==1 & merged$politics2nd==1], na.rm=T), n1=767, n2=776, low_eqbound_d=-0.2, high_eqbound_d=0.2, alpha = 0.05)
#cultural
TOSTtwo(m1=mean(merged$cult[merged$passed==1 & merged$politics2nd==0], na.rm=T), m2=mean(merged$cult[merged$passed==1 & merged$politics2nd==1], na.rm=T), sd1=sd(merged$cult[merged$passed==1 & merged$politics2nd==0], na.rm=T), sd2=sd(merged$cult[merged$passed==1 & merged$politics2nd==1], na.rm=T), n1=767, n2=776, low_eqbound_d=-0.2, high_eqbound_d=0.2, alpha = 0.05)
#economic
TOSTtwo(m1=mean(merged$econ[merged$passed==1 & merged$politics2nd==0], na.rm=T), m2=mean(merged$econ[merged$passed==1 & merged$politics2nd==1], na.rm=T), sd1=sd(merged$econ[merged$passed==1 & merged$politics2nd==0], na.rm=T), sd2=sd(merged$econ[merged$passed==1 & merged$politics2nd==1], na.rm=T), n1=767, n2=776, low_eqbound_d=-0.2, high_eqbound_d=0.2, alpha = 0.05)

#ideology
TOSTtwo(m1=mean(merged$libcon01[merged$passed==1 & merged$politics2nd==0], na.rm=T), m2=mean(merged$libcon01[merged$passed==1 & merged$politics2nd==1], na.rm=T), sd1=sd(merged$libcon01[merged$passed==1 & merged$politics2nd==0], na.rm=T), sd2=sd(merged$libcon01[merged$passed==1 & merged$politics2nd==1], na.rm=T), n1=767, n2=776, low_eqbound_d=-0.2, high_eqbound_d=0.2, alpha = 0.05)

#pid
TOSTtwo(m1=mean(merged$pid01[merged$passed==1 & merged$politics2nd==0], na.rm=T), m2=mean(merged$pid01[merged$passed==1 & merged$politics2nd==1], na.rm=T), sd1=sd(merged$pid01[merged$passed==1 & merged$politics2nd==0], na.rm=T), sd2=sd(merged$pid01[merged$passed==1 & merged$politics2nd==1], na.rm=T), n1=767, n2=776, low_eqbound_d=-0.2, high_eqbound_d=0.2, alpha = 0.05)

#approval
TOSTtwo(m1=mean(merged$approval01[merged$passed==1 & merged$politics2nd==0], na.rm=T), m2=mean(merged$approval01[merged$passed==1 & merged$politics2nd==1], na.rm=T), sd1=sd(merged$approval01[merged$passed==1 & merged$politics2nd==0], na.rm=T), sd2=sd(merged$approval01[merged$passed==1 & merged$politics2nd==1], na.rm=T), n1=767, n2=776, low_eqbound_d=-0.2, high_eqbound_d=0.2, alpha = 0.05)


#SI 5.6 - Outcome neutral quality checks-------------------
ideology_normal<-list()
ideology_normal[[1]]<-lm(zero1(general)~gender+age+race+education, subset(merged, passed==1))
ideology_normal[[2]]<-lm(zero1(symbolic)~gender+age+race+education, subset(merged, passed==1))
ideology_normal[[3]]<-lm(zero1(cult)~gender+age+race+education, subset(merged, passed==1))
ideology_normal[[4]]<-lm(zero1(econ)~gender+age+race+education, subset(merged, passed==1))
ideology_normal[[5]]<-lm(libcon01~gender+age+race+education, subset(merged, passed==1))
ideology_normal[[6]]<-lm(pid01~gender+age+race+education, subset(merged, passed==1))
ideology_normal[[7]]<-lm(approval01~gender+age+race+education, subset(merged, passed==1))


stargazer(ideology_normal[[1]], ideology_normal[[2]], ideology_normal[[5]], ideology_normal[[6]], ideology_normal[[7]], ideology_normal[[3]], ideology_normal[[4]], column.separate = c(1,1,1,1,1,1,1), dep.var.caption  = "Outcome neutral quality checks", dep.var.labels = c("General", "Symbolic", "Ideology", "Party", "Approval", "Cultural", "Economic"), covariate.labels = c("Gender: Male", "Gender: Other", "Age", "Race: Hispanic", "Race: Other", "Race: White", "Edu: High school", "Edu: Some college"), title="Outcome neutral quality checks", no.space = TRUE, align=TRUE, model.numbers = FALSE, omit.stat=c("LL","ser","f", "adj.rsq"), star.char = c("+", "*"), star.cutoffs = c(.1, 0.05), notes = c("+ p<.1; * p<0.05)"),  font.size="tiny", notes.append = F, out="Results/Ideology_normal.tex", label="tab:ideology_normal", digits=2)

#SI 5.7 - Output belonging to figure 5: preregistered --------
merged_stargazer <-dplyr::select(merged_l, value, general, symbolic,cult, econ, libcon01, pid01, approval01, politics2nd,variable,gender,age,race,education,id, passed)
merged_stargazer<-subset(merged_stargazer, passed==1)
merged_stargazer<-merged_stargazer[complete.cases(merged_stargazer), ]

overal <- lm(value~general*politics2nd+variable+gender+age+race+education,merged_stargazer)
symbolic <- lm(value~symbolic*politics2nd+variable+gender+age+race+education,merged_stargazer)
pid <- lm(value~pid01*politics2nd+variable+gender+age+race+education,merged_stargazer)
ideo <- lm(value~libcon01*politics2nd+variable+gender+age+race+education,merged_stargazer)
approval <- lm(value~approval01*politics2nd+variable+gender+age+race+education,merged_stargazer)
cult <- lm(value~cult*politics2nd+variable+gender+age+race+education,merged_stargazer)
econ <- lm(value~econ*politics2nd+variable+gender+age+race+education,merged_stargazer)

stargazer(overal, symbolic, pid, ideo, approval, cult, econ, se=starprep(overal, symbolic, pid, ideo, approval, cult, econ, clusters = merged_stargazer$id, se_type = "stata"), column.separate = c(1,1,1,1,1,1,1), column.labels = c("General", "Symbolic", "Party identity", "Ideology", "Approval", "Culture", "Economic"), covariate.labels = c("General", "Symbolic", "Party idenity", "Ideology", "Trump approval", "Culture", "Economic", "Politics Treatment", "Conscientiousness", "Gender: Male", "Gender: Other", "Age", "Race: Hispanic", "Race: Other", "Race: White", "Education: high school", "Education:  some college","General * Politics", "Symbolic * Politics", "Party * Politics", "Ideology * Politics", "Approval * Politics", "Cultural * Politics", "Economic * Politics", "Constant"), title="Results belonging to Figure 5 in the main text", align=TRUE, no.space = TRUE, model.numbers = FALSE, omit.stat=c("LL","ser","f", "adj.rsq"), star.char = c("+", "*"), star.cutoffs = c(.2, 0.1), notes = c("+ p(one-sided)<.1; * p(one-sided)<0.05)"),font.size = "tiny",  notes.append = F, out="Results/Experiment_results.tex", label="tab:experiment_results", digits=2,dep.var.caption = "",dep.var.labels.include = F)

#SI 5.8 - Trichotomous variables--------------------
merged_stargazer <-dplyr::select(merged_l, value, general, symbolic,cult, econ, libcon01, pid01, approval01, politics2nd,variable,gender,age,race,education,id, passed)
merged_stargazer<-subset(merged_stargazer, passed==1)
#merged_stargazer<-select(merged_stargazer,-c(16))
merged_stargazer<-merged_stargazer[complete.cases(merged_stargazer), ]

merged_stargazer$econ3 <- as.factor(as.numeric(gtools::quantcut(merged_stargazer$econ,q=seq(0,1,by=.33))))
merged_stargazer$cult3 <- as.factor(as.numeric(gtools::quantcut(merged_stargazer$cult,q=seq(0,1,by=.33))))
merged_stargazer$symbolic3 <- as.factor(as.numeric(gtools::quantcut(merged_stargazer$symbolic,q=seq(0,1,by=.33))))
merged_stargazer$general3 <- as.factor(as.numeric(gtools::quantcut(merged_stargazer$general,q=seq(0,1,by=.33))))
merged_stargazer$libcon3 <- as.factor(as.numeric(gtools::quantcut(merged_stargazer$libcon01,q=seq(0,1,by=.33))))
merged_stargazer$pid3 <- as.factor(as.numeric(gtools::quantcut(merged_stargazer$pid01,q=seq(0,1,by=.33))))
merged_stargazer$approval3 <- as.factor(as.numeric(gtools::quantcut(merged_stargazer$approval01,q=seq(0,1,by=.33))))

overal <- lm_robust(value~general3*politics2nd+variable+gender+age+race+education, merged_stargazer,se_type = "stata",clusters = id)
sym <- lm_robust(value~symbolic3*politics2nd+variable+gender+age+race+education, merged_stargazer,se_type = "stata",clusters = id)
ideo <- lm_robust(value~libcon3*politics2nd+variable+gender+age+race+education, merged_stargazer,se_type = "stata",clusters = id)
party <- lm_robust(value~pid3*politics2nd+variable+gender+age+race+education, merged_stargazer,se_type = "stata",clusters = id)
approval <- lm_robust(value~approval3*politics2nd+variable+gender+age+race+education, merged_stargazer,se_type = "stata",clusters = id)
cult <- lm_robust(value~cult3*politics2nd+variable+gender+age+race+education, merged_stargazer,se_type = "stata",clusters = id)
econ <- lm_robust(value~econ3*politics2nd+variable+gender+age+race+education, merged_stargazer,se_type = "stata",clusters = id)

a <- summary(margins(overal,merged_stargazer,variables ="politics2nd" ,at=list(general3=c("1","2","3"))),level=.90)
b <- summary(margins(sym,merged_stargazer,variables ="politics2nd" ,at=list(symbolic3=c("1","2","3"))),level=.9)
c <- summary(margins(ideo,merged_stargazer,variables ="politics2nd" ,at=list(libcon3=c("1","2","3"))),level=.9)
d <- summary(margins(party,merged_stargazer,variables ="politics2nd" ,at=list(pid3=c("1","2","3"))),level=.9)
e <- summary(margins(approval,merged_stargazer,variables ="politics2nd" ,at=list(approval3=c("1","2","3"))),level=.9)
f <- summary(margins(econ,merged_stargazer,variables ="politics2nd" ,at=list(econ3=c("1","2","3"))),level=.9)
g <-summary(margins(cult,merged_stargazer,variables ="politics2nd" ,at=list(cult3=c("1","2","3"))),level=.9)

colnames(a)[2] <- "Tertile"
colnames(b)[2] <- "Tertile"
colnames(c)[2] <- "Tertile"
colnames(d)[2] <- "Tertile"
colnames(e)[2] <- "Tertile"
colnames(f)[2] <- "Tertile"
colnames(g)[2] <- "Tertile"

out <- rbind(a,b,c,d,e,f,g)
out$int <- c("General", "Symbolic", "Ideology", "Party identity", "Approval","Culture","Economic")
out$int <- fct_relevel(out$int, "General", "Symbolic", "Ideology", "Party identity", "Approval","Culture","Economic")
out$split <- c(rep(c("Low (<33%)", "Middle (33%-66%)", "High(>66%)"),7))
out$split <- fct_relevel(out$split, "Low (<33%)", "Middle (33%-66%)", "High(>66%)")

ggplot(out,aes(x=split,y=AME))+geom_pointrange(aes(ymin=lower,ymax=upper))+facet_wrap(~int, ncol=2)+theme_bw()+ xlab("Tertile split of ideology variables") + ylab("Average Marginal Effect of Politics\n treatment (vs. internet) on wave 2 personality")
ggsave("Results/big5linearity.pdf",width=8,height=8)

#SI 5.9 - Openeness seperate--------------
#general
overal <- (lm_robust(opennessr~general*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(overal,variables = "general",at=list(politics2nd=c(0,1)))
overal_o <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='General'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='General'))

#cultural
cult_model <- (lm_robust(opennessr~cult*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(cult_model,variables = "cult",at=list(politics2nd=c(0,1)))
cult_o <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Culture'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Culture'))

#economic
econ_model <- (lm_robust(opennessr~econ*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(econ_model,variables = "econ",at=list(politics2nd=c(0,1)))
econ_o <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Economic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Economic'))

#symbolic
symbolic_model <- (lm_robust(opennessr~symbolic*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(symbolic_model,variables = "symbolic",at=list(politics2nd=c(0,1)))
symbolic_o <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Symbolic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Symbolic'))

#pid
pid_model <- (lm_robust(opennessr~pid01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(pid_model,variables = "pid01",at=list(politics2nd=c(0,1)))
pid_o <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Party Identity'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Party Identity'))

#ideology
ideology_model <- (lm_robust(opennessr~libcon01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(ideology_model,variables = "libcon01",at=list(politics2nd=c(0,1)))
ideology_o <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Ideology'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Ideology'))

#approval
approval_model <- (lm_robust(opennessr~approval01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(approval_model,variables = "approval01",at=list(politics2nd=c(0,1)))
approval_o <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Trump approval'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Trump approval'))

#build plot
forplot <- rbind(overal_o, symbolic_o, pid_o, ideology_o, approval_o, cult_o, econ_o)


pvalues<-c(summary(overal)$coefficients[12,4]/2, summary(symbolic_model)$coefficients[12,4]/2, summary(pid_model)$coefficients[12,4]/2,  summary(ideology_model)$coefficients[12,4]/2, summary(approval_model)$coefficients[12,4]/2, summary(cult_model)$coefficients[12,4]/2,  summary(econ_model)$coefficients[12,4]/2)

annotation_df <- data.frame(predictor=c("General", "Symbolic", "Party Identity", "Ideology", "Trump approval", "Culture", "Economic"),
                            start=c("Internet"),
                            end=c("Politics"),
                            y=c(.26),
                            label=round(pvalues, digits=2)) #if p-values with stars just p-values
forplot$predictor <- factor(forplot$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))
annotation_df$predictor <- factor(annotation_df$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))

main<-ggplot(forplot,aes(y=ame,x=condition))+facet_wrap(~predictor,scales = "fixed", ncol=2) + xlab("Condition") + ylab("Average Marginal Effect of wave 1 ideology on wave 2 Openness to Experience") +geom_pointrange(aes(ymin=lower,ymax=upper))+theme_bw() + geom_signif(data=annotation_df, aes(xmin=start, xmax=end, annotations=label, y_position=y), textsize = 3, vjust = -0.2, manual=TRUE)+ylim(0,.27)
ggsave(main, file="Results/openness_results.pdf")

#SI 5.9 - Conscientiousness seperate--------------
#general
overal <- (lm_robust(conscientiousness~general*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(overal,variables = "general",at=list(politics2nd=c(0,1)))
overal_c <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='General'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='General'))

#cultural
cult_model <- (lm_robust(conscientiousness~cult*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(cult_model,variables = "cult",at=list(politics2nd=c(0,1)))
cult_c <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Culture'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Culture'))

#economic
econ_model <- (lm_robust(conscientiousness~econ*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(econ_model,variables = "econ",at=list(politics2nd=c(0,1)))
econ_c <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Economic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Economic'))

#symbolic
symbolic_model <- (lm_robust(conscientiousness~symbolic*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(symbolic_model,variables = "symbolic",at=list(politics2nd=c(0,1)))
symbolic_c <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Symbolic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Symbolic'))

#pid
pid_model <- (lm_robust(conscientiousness~pid01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(pid_model,variables = "pid01",at=list(politics2nd=c(0,1)))
pid_c <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Party Identity'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Party Identity'))

#ideology
ideology_model <- (lm_robust(conscientiousness~libcon01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(ideology_model,variables = "libcon01",at=list(politics2nd=c(0,1)))
ideology_c <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Ideology'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Ideology'))

#approval
approval_model <- (lm_robust(conscientiousness~approval01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(approval_model,variables = "approval01",at=list(politics2nd=c(0,1)))
approval_c <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Trump approval'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Trump approval'))

#build plot
forplot <- rbind(overal_c, symbolic_c, pid_c, ideology_c, approval_c, cult_c, econ_c)

pvalues<-c(summary(overal)$coefficients[12,4]/2, summary(symbolic_model)$coefficients[12,4]/2, summary(pid_model)$coefficients[12,4]/2,  summary(ideology_model)$coefficients[12,4]/2, summary(approval_model)$coefficients[12,4]/2, summary(cult_model)$coefficients[12,4]/2,  summary(econ_model)$coefficients[12,4]/2)

annotation_df <- data.frame(predictor=c("General", "Symbolic", "Party Identity", "Ideology", "Trump approval", "Culture", "Economic"),
                            start=c("Internet"),
                            end=c("Politics"),
                            y=c(.26),
                            label=round(pvalues, digits=2)) #if p-values with stars just p-values
forplot$predictor <- factor(forplot$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))
annotation_df$predictor <- factor(annotation_df$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))

main<-ggplot(forplot,aes(y=ame,x=condition))+facet_wrap(~predictor,scales = "fixed", ncol=2) + xlab("Condition") + ylab("Average Marginal Effect of wave 1 ideology on wave 2 Conscientiousness") +geom_pointrange(aes(ymin=lower,ymax=upper))+theme_bw() + geom_signif(data=annotation_df, aes(xmin=start, xmax=end, annotations=label, y_position=y), textsize = 3, vjust = -0.2, manual=TRUE)+ylim(-.05,.3)
ggsave(main, file="Results/conscientiousness_results.pdf")


#SI 5.10 - Neuroticism: exploratory -------------------------
merged_complete$neuroticismr<-1-merged_complete$neuroticism
#general
overal <- (lm_robust(neuroticismr~general*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(overal,variables = "general",at=list(politics2nd=c(0,1)))
overal_n <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='General'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='General'))

#cultural
cult_model <- (lm_robust(neuroticismr~cult*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(cult_model,variables = "cult",at=list(politics2nd=c(0,1)))
cult_n <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Culture'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Culture'))

#economic
econ_model <- (lm_robust(neuroticismr~econ*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(econ_model,variables = "econ",at=list(politics2nd=c(0,1)))
econ_n <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Economic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Economic'))

#symbolic
symbolic_model <- (lm_robust(neuroticismr~symbolic*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(symbolic_model,variables = "symbolic",at=list(politics2nd=c(0,1)))
symbolic_n <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Symbolic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Symbolic'))

#pid
pid_model <- (lm_robust(neuroticismr~pid01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(pid_model,variables = "pid01",at=list(politics2nd=c(0,1)))
pid_n <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Party Identity'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Party Identity'))

#ideology
ideology_model <- (lm_robust(neuroticismr~libcon01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(ideology_model,variables = "libcon01",at=list(politics2nd=c(0,1)))
ideology_n <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Ideology'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Ideology'))

#approval
approval_model <- (lm_robust(neuroticismr~approval01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(approval_model,variables = "approval01",at=list(politics2nd=c(0,1)))
approval_n <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Trump approval'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Trump approval'))

#build plot
forplot <- rbind(overal_n, symbolic_n, pid_n, ideology_n, approval_n, cult_n, econ_n)

pvalues<-c(summary(overal)$coefficients[12,4]/2, summary(symbolic_model)$coefficients[12,4]/2, summary(pid_model)$coefficients[12,4]/2,  summary(ideology_model)$coefficients[12,4]/2, summary(approval_model)$coefficients[12,4]/2, summary(cult_model)$coefficients[12,4]/2,  summary(econ_model)$coefficients[12,4]/2)

annotation_df <- data.frame(predictor=c("General", "Symbolic", "Party Identity", "Ideology", "Trump approval", "Culture", "Economic"),
                            start=c("Internet"),
                            end=c("Politics"),
                            y=c(.26),
                            label=round(pvalues, digits=2)) #if p-values with stars just p-values
forplot$predictor <- factor(forplot$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))
annotation_df$predictor <- factor(annotation_df$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))

main<-ggplot(forplot,aes(y=ame,x=condition))+facet_wrap(~predictor,scales = "fixed") + xlab("Condition") + ylab("Average Marginal Effect of wave 1 ideology on wave 2 Neuroticism") +geom_pointrange(aes(ymin=lower,ymax=upper))+theme_bw() + geom_signif(data=annotation_df, aes(xmin=start, xmax=end, annotations=label, y_position=y), textsize = 3, vjust = -0.2, manual=TRUE)+ylim(0,.3) + geom_hline(yintercept = 0, linetype="dashed")
ggsave(main, file="Results/neuroticism_results.pdf")

#SI 5.10 - Agreeablenesss: exploratory --------------------
merged_complete$agreeablenessr<-1-merged_complete$agreeableness
#general
overal <- (lm_robust(agreeablenessr~general*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(overal,variables = "general",at=list(politics2nd=c(0,1)))
overal_a <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='General'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='General'))

#cultural
cult_model <- (lm_robust(agreeablenessr~cult*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(cult_model,variables = "cult",at=list(politics2nd=c(0,1)))
cult_a <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Culture'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Culture'))

#economic
econ_model <- (lm_robust(agreeablenessr~econ*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(econ_model,variables = "econ",at=list(politics2nd=c(0,1)))
econ_a <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Economic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Economic'))

#symbolic
symbolic_model <- (lm_robust(agreeablenessr~symbolic*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(symbolic_model,variables = "symbolic",at=list(politics2nd=c(0,1)))
symbolic_a <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Symbolic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Symbolic'))

#pid
pid_model <- (lm_robust(agreeablenessr~pid01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(pid_model,variables = "pid01",at=list(politics2nd=c(0,1)))
pid_a <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Party Identity'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Party Identity'))

#ideology
ideology_model <- (lm_robust(agreeablenessr~libcon01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(ideology_model,variables = "libcon01",at=list(politics2nd=c(0,1)))
ideology_a <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Ideology'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Ideology'))

#approval
approval_model <- (lm_robust(agreeablenessr~approval01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(approval_model,variables = "approval01",at=list(politics2nd=c(0,1)))
approval_a <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Trump approval'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Trump approval'))

#build plot
forplot <- rbind(overal_a, symbolic_a, pid_a, ideology_a, approval_a, cult_a, econ_a)


pvalues<-c(summary(overal)$coefficients[12,4]/2, summary(symbolic_model)$coefficients[12,4]/2, summary(pid_model)$coefficients[12,4]/2,  summary(ideology_model)$coefficients[12,4]/2, summary(approval_model)$coefficients[12,4]/2, summary(cult_model)$coefficients[12,4]/2,  summary(econ_model)$coefficients[12,4]/2)

annotation_df <- data.frame(predictor=c("General", "Symbolic", "Party Identity", "Ideology", "Trump approval", "Culture", "Economic"),
                            start=c("Internet"),
                            end=c("Politics"),
                            y=c(.16),
                            label=round(pvalues, digits=2)) #if p-values with stars just p-values
forplot$predictor <- factor(forplot$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))
annotation_df$predictor <- factor(annotation_df$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))

main<-ggplot(forplot,aes(y=ame,x=condition))+facet_wrap(~predictor,scales = "fixed") + xlab("Condition") + ylab("Average Marginal Effect of wave 1 ideology on wave 2 Agreeableness") +geom_pointrange(aes(ymin=lower,ymax=upper))+theme_bw() + geom_signif(data=annotation_df, aes(xmin=start, xmax=end, annotations=label, y_position=y), textsize = 3, vjust = -0.2, manual=TRUE)+ylim(-.07,.17) + geom_hline(yintercept = 0, linetype="dashed")
ggsave(main, file="Results/agreeableness_results.pdf")

#SI 5.10 - Extraversion: exploratory -------------------------
#general
overal <- (lm_robust(extraversion~general*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(overal,variables = "general",at=list(politics2nd=c(0,1)))
overal_e <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='General'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='General'))

#cultural
cult_model <- (lm_robust(extraversion~cult*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(cult_model,variables = "cult",at=list(politics2nd=c(0,1)))
cult_e <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Culture'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Culture'))

#economic
econ_model <- (lm_robust(extraversion~econ*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(econ_model,variables = "econ",at=list(politics2nd=c(0,1)))
econ_e <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Economic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Economic'))

#symbolic
symbolic_model <- (lm_robust(extraversion~symbolic*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(symbolic_model,variables = "symbolic",at=list(politics2nd=c(0,1)))
symbolic_e <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Symbolic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Symbolic'))

#pid
pid_model <- (lm_robust(extraversion~pid01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(pid_model,variables = "pid01",at=list(politics2nd=c(0,1)))
pid_e <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Party Identity'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Party Identity'))

#ideology
ideology_model <- (lm_robust(extraversion~libcon01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(ideology_model,variables = "libcon01",at=list(politics2nd=c(0,1)))
ideology_e <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Ideology'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Ideology'))

#approval
approval_model <- (lm_robust(extraversion~approval01*politics2nd+gender+age+race+education,as.data.frame(subset(merged_complete,passed==1)),se_type = "stata",clusters = id))
out1 <- margins(approval_model,variables = "approval01",at=list(politics2nd=c(0,1)))
approval_e <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Trump approval'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Trump approval'))

#build plot
forplot <- rbind(overal_e, symbolic_e, pid_e, ideology_e, approval_e, cult_e, econ_e)

pvalues<-c(summary(overal)$coefficients[12,4]/2, summary(symbolic_model)$coefficients[12,4]/2, summary(pid_model)$coefficients[12,4]/2,  summary(ideology_model)$coefficients[12,4]/2, summary(approval_model)$coefficients[12,4]/2, summary(cult_model)$coefficients[12,4]/2,  summary(econ_model)$coefficients[12,4]/2)

annotation_df <- data.frame(predictor=c("General", "Symbolic", "Party Identity", "Ideology", "Trump approval", "Culture", "Economic"),
                            start=c("Internet"),
                            end=c("Politics"),
                            y=c(.22),
                            label=round(pvalues, digits=2)) #if p-values with stars just p-values
forplot$predictor <- factor(forplot$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))
annotation_df$predictor <- factor(annotation_df$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))

main<-ggplot(forplot,aes(y=ame,x=condition))+facet_wrap(~predictor,scales = "fixed") + xlab("Condition") + ylab("Average Marginal Effect of wave 1 ideology on wave 2 Extraversion") +geom_pointrange(aes(ymin=lower,ymax=upper))+theme_bw() + geom_signif(data=annotation_df, aes(xmin=start, xmax=end, annotations=label, y_position=y), textsize = 3, vjust = -0.2, manual=TRUE)+ylim(0,.25) + geom_hline(yintercept = 0, linetype="dashed")
ggsave(main, file="Results/extraversion_results.pdf")


#SI 5.11 - strategy as employed in the paper: Including those that failed the attention check-----------------------

#general ideology
overal <- lm_robust(value~general*politics2nd+variable+gender+age+race+education+passed,as.data.frame(subset(merged_l)),clusters = id,se_type = "stata")
out1 <- margins(overal,variables = "general",at=list(politics2nd=c(0,1)))
general <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='General'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='General'))

#Symbolic
overal_symbolic <- (lm_robust(value~symbolic*politics2nd+variable+gender+age+race+education+passed,subset(merged_l),clusters = id,se_type = "stata"))
out1 <- margins(overal_symbolic,variables = "symbolic",at=list(politics2nd=c(0,1)))
symbolic <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Symbolic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Symbolic'))

#pid
overal_pid <- (lm_robust(value~pid01*politics2nd+variable+gender+age+race+education+passed,subset(merged_l),clusters = id,se_type = "stata"))
out1 <- margins(overal_pid,variables = "pid01",at=list(politics2nd=c(0,1)))
pid <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Party Identity'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Party Identity'))

#ideology
overal_ideology <- (lm_robust(value~libcon01*politics2nd+variable+gender+age+race+education+passed,subset(merged_l),clusters = id,se_type = "stata"))
out1 <- margins(overal_ideology,variables = "libcon01",at=list(politics2nd=c(0,1)))
ideology <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Ideology'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Ideology'))

#approval
overal_approval <- (lm_robust(value~approval01*politics2nd+variable+gender+age+race+education+passed,subset(merged_l),clusters = id,se_type = "stata"))
out1 <- margins(overal_approval,variables = "approval01",at=list(politics2nd=c(0,1)))
approval <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Trump approval'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Trump approval'))

#cultural conservatsimn
overal_cult <- (lm_robust(value~cult*politics2nd+variable+gender+age+race+education+passed,subset(merged_l),clusters = id,se_type = "stata"))
out1 <- margins(overal_cult,variables = "cult",at=list(politics2nd=c(0,1)))
culture <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Culture'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Culture'))

#economic conservatism
overal_econ <- (lm_robust(value~econ*politics2nd+variable+gender+age+race+education+passed,subset(merged_l),clusters = id,se_type = "stata"))
summary(overal_econ)
out1 <- margins(overal_econ,variables = "econ",at=list(politics2nd=c(0,1)))
econ <- rbind(data.frame(ame=summary(out1,level=.9)[1,3],lower=summary(out1,level=.9)[1,7],upper=summary(out1,level=.9)[1,8],condition="Internet",predictor='Economic'), data.frame(ame=summary(out1,level=.9)[2,3],lower=summary(out1,level=.9)[2,7],upper=summary(out1,level=.9)[2,8],condition="Politics",predictor='Economic'))

#build plot
forplot <- rbind(general, symbolic, pid, ideology, approval, culture, econ)
#extract p-values and calculate one-sided p-values
pvalues<-c(summary(overal)$coefficients[14,4]/2, summary(overal_symbolic)$coefficients[14,4]/2, summary(overal_pid)$coefficients[14,4]/2, summary(overal_ideology)$coefficients[14,4]/2,summary(overal_approval)$coefficients[14,4]/2,  summary(overal_cult)$coefficients[14,4]/2,  summary(overal_econ)$coefficients[14,4]/2)

#pvalues<-ifelse(pvalues<.05,"*",     ifelse(pvalues>.05 & pvalues<.1,"+", "NS"))

annotation_df <- data.frame(predictor=c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"),
                            start=c("Internet"),
                            end=c("Politics"),
                            y=c(.22),
                            label=round(pvalues,3)) #if p-values with stars just p-values
annotation_df$predictor <- factor(annotation_df$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))
forplot$predictor <- factor(forplot$predictor, level = c("General", "Symbolic", "Party Identity", "Ideology","Trump approval", "Culture", "Economic"))

main<-ggplot(forplot,aes(y=ame,x=condition))+facet_wrap(~predictor,scales = "fixed", ncol=2) + xlab("Condition") + ylab("Average Marginal Effect") +geom_pointrange(aes(ymin=lower,ymax=upper))+theme_bw(base_size = 25) + geom_signif(data=annotation_df, aes(xmin=start, xmax=end, annotations=label, y_position=y), textsize = 3, vjust = -0.2, manual=TRUE)+ylim(0.00,.25)

ggsave(main, file="Results/fig4_failed_attention.pdf",width=8,height=10)


#SI 9.1 - results conditional upon age: linear--------------
age_cond<-list()
age_cond[[1]] <- lm_robust(value~general*politics2nd*age+variable+gender+race+education,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
age_cond[[2]] <- lm_robust(value~symbolic*politics2nd*age+variable+gender+race+education,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
age_cond[[3]] <- lm_robust(value~pid01*politics2nd*age+variable+gender+race+education,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
age_cond[[4]] <- lm_robust(value~libcon01*politics2nd*age+variable+gender+race+education,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
age_cond[[5]] <- lm_robust(value~approval01*politics2nd*age+variable+gender+race+education,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
age_cond[[6]] <- lm_robust(value~cult*politics2nd*age+variable+gender+race+education,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
age_cond[[7]] <- lm_robust(value~econ*politics2nd*age+variable+gender+race+education,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")

#SI 9.2 - conditional effects of educatoin ------
merged_l$edu01<-ifelse(merged_l$education=="College+", 1,0)
edu_cond<-list()
edu_cond[[1]] <- lm_robust(value~general*politics2nd*edu01+age+variable+gender+race,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
edu_cond[[2]] <- lm_robust(value~symbolic*politics2nd*edu01+age+variable+gender+race,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
edu_cond[[3]] <- lm_robust(value~pid01*politics2nd*edu01+age+variable+gender+race,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
edu_cond[[4]] <- lm_robust(value~libcon01*politics2nd*edu01+age+variable+gender+race,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
edu_cond[[5]] <- lm_robust(value~approval01*politics2nd*edu01+age+variable+gender+race,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
edu_cond[[6]] <- lm_robust(value~cult*politics2nd*edu01+age+variable+gender+race,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")
edu_cond[[7]] <- lm_robust(value~econ*politics2nd*edu01+age+variable+gender+race,as.data.frame(subset(merged_l,passed==1)),clusters = id,se_type = "stata")

